package com.a3dweijing.wetouch2.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.a3dweijing.wetouch2.Activity.MainActivity;
import com.a3dweijing.wetouch2.Common.CallBack;
import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.adapter.HomeVideoAdapter;
import com.a3dweijing.wetouch2.bean.HomeVideoMsg;
import com.a3dweijing.wetouch2.datasave.Singleton;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/3/19. 首页
 */

public class HomePageFragment extends Fragment implements CallBack
{
    MainActivity activity;
    
    @BindView(R.id.back)
    ImageView back;
    
    @BindView(R.id.top_text)
    TextView topText;
    
    @BindView(R.id.top_right)
    TextView topRight;
    
    @BindView(R.id.top_all)
    RelativeLayout topAll;
    
    @BindView(R.id.resaut_status_img)
    ImageView resautStatusImg;
    
    @BindView(R.id.resaut_status)
    LinearLayout resautStatus;
    
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    
    Unbinder unbinder;
    
    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        this.activity = (MainActivity)activity;
    }
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState)
    {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.home_page_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initData();
        return view;
    }
    
    private void initView()
    {
        topText.setText("微触");
        back.setVisibility(View.GONE);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        
    }
    
    private void initData()
    {
        
        OkGo.post("http://admin.3dweijing.com/admin.php/api/shopping/video").execute(new StringCallback()
        {
            @Override
            public void onSuccess(String s, Call call, Response response)
            {
                HomeVideoMsg msg = new Gson().fromJson(s, HomeVideoMsg.class);
                Log.e("--", "onSuccess: " + s);
                if (msg.isFlag())
                {
                    if (msg.getVideo() != null && msg.getVideo().size() > 0)
                    {
                        resautStatus.setVisibility(View.GONE);
                        if (Singleton.getInstance().getIsfrist())
                        {
                            // 不自动跳出视频 activity.setFrameShow("https://lfriend.oss-cn-shenzhen.aliyuncs.com/upload/"
                            // + msg.getVideo().get(0).getVideoUrl());
                            Singleton.getInstance().setIsfrist(false);
                        }
                        recyclerView.setAdapter(new HomeVideoAdapter(msg.getVideo(), HomePageFragment.this));
                    }
                    else
                    {
                        resautStatus.setVisibility(View.VISIBLE);
                    }
                }
                else
                {
                    resautStatus.setVisibility(View.VISIBLE);
                }
            }
        });
        
    }
    
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        unbinder.unbind();
        
    }
    
    @Override
    public void doNext(int what, Object obj)
    {
        HomeVideoMsg.VideoBean bean = (HomeVideoMsg.VideoBean)obj;
        if (bean != null)
        {
            activity.setFrameShow("https://lfriend.oss-cn-shenzhen.aliyuncs.com/upload/" + bean.getVideoUrl());
        }
    }
}
